﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyShopThoiTrang.QuanLyShopThoiTrang_BUS_WS;
using COMExcel = Microsoft.Office.Interop.Excel;

namespace QuanLyShopThoiTrang
{
    public partial class InHoaDonBanHang : Form
    {
        COMExcel.Application exApp;
        COMExcel.Workbook exBook;
        bool iPrint = false;
        QLSTT_BUSSoapClient ws = new QLSTT_BUSSoapClient();
        NhanVien nv;
        DateTime d;
        double sum;
        string km;
        public InHoaDonBanHang()
        {
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            /*if (iPrint)
            {
                exBook.Close(false, false, false);
                exApp.Quit();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(exBook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(exApp);
            }*/
            Close();
        }

        public void Set(NhanVien n, DataGridView data, DateTime dt, double sum, string km)
        {
            nv = n;
            d = dt;
            this.sum = sum;
            this.km = km;
            if(km != "")
                label6.Text = km;
            textBox1.Text = n.NhanVien_TenDayDu;
            textBox2.Text = dt.Date.ToString();
            foreach (DataGridViewRow row in data.Rows)
            {
                SanPham sp = new SanPham();
                sp = (SanPham)row.Tag;

                dataGridView2.Rows.Add(row.Cells[1].Value, row.Cells[2].Value, row.Cells[3].Value, row.Cells[9].Value, (sp.SanPham_GiaUuDai == null ? sp.SanPham_GiaKhiBan : sp.SanPham_GiaUuDai));
                dataGridView2.Rows[dataGridView2.Rows.Count - 1].Tag = sp;
            }
            textBox3.Text = sum.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            HoaDonBanHang hdbh = new HoaDonBanHang();
            hdbh.HDBH_NgayLap = d;
            hdbh.MaNhanVien = nv.MaNhanVien;
            hdbh.HDBH_ThanhTien = sum;
            hdbh.HDBH_TrangThai = "Su dung";
            hdbh.HDBH_KhuyenMai = km;

            if (!ws.HoaDonBanHang_Them(hdbh))
            {
                MessageBox.Show("Thất bại");
                return;
            }

            hdbh = new HoaDonBanHang();
            hdbh = ws.HoaDonBanHang_LayHDGanNhat();

            foreach (DataGridViewRow row in dataGridView2.Rows)
            {
                SanPham sp = (SanPham)row.Tag;
                ChiTietHoaDonBanHang cthdbh = new ChiTietHoaDonBanHang();
                cthdbh.MaHoaDon = hdbh.MaHoaDon;
                cthdbh.MaSanPham = sp.MaSanPham;
                if (sp.SanPham_GiaUuDai == null)
                    cthdbh.CTHDBH_DonGia = sp.SanPham_GiaKhiBan;
                else
                    cthdbh.CTHDBH_DonGia = sp.SanPham_GiaUuDai;
                cthdbh.CTHDBH_SoLuong = (int)row.Cells[3].Value;
                cthdbh.CTHDBH_TrangThai = "Su dung";

                ws.ChiTietHoaDonBanHang_Them(cthdbh);
                sp.SanPham_SoLuong -= (int)row.Cells[3].Value;
                ws.SanPham_CapNhat(sp);
            }
            MessageBox.Show("Thành công.");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            /*if(iPrint)
            {
                exBook.Close(false, false, false);
                exApp.Quit();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(exBook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(exApp);
            }*/
            iPrint = true;
            // Đoạn code tham khảo ở trang http://forums.congdongcviet.com/showthread.php?t=13061
            // Khởi động chtr Excell
            exApp = new COMExcel.Application();

            // Thêm file temp xls
            exBook = exApp.Workbooks.Add(COMExcel.XlWBATemplate.xlWBATWorksheet);

            // Lấy sheet 1.
            COMExcel.Worksheet exSheet = (COMExcel.Worksheet)exBook.Worksheets[1];

            exSheet.Activate();
            exSheet.Name = "Hóa đơn bán hàng";

            // Range là ô [1,1] (A1)
            COMExcel.Range r = (COMExcel.Range)exSheet.get_Range("I1", "I1");

            // Ghi dữ liệu
            r.Font.Bold = true;
            r.Font.Size = 20;
            r.Value2 = "HÓA ĐƠN BÁN HÀNG";

            r = (COMExcel.Range)exSheet.get_Range("H2", "H2");
            r.Font.Bold = true;
            r.Font.Size = 12;
            r.Value2 = "Tên nhân viên: " + nv.NhanVien_TenDayDu;

            r = (COMExcel.Range)exSheet.get_Range("H3", "H3");
            r.Font.Bold = true;
            r.Font.Size = 12;
            r.Value2 = "Ngày lập: " + d.Date.ToString();

            r = (COMExcel.Range)exSheet.get_Range("H5", "H5");
            r.Font.Bold = true;
            r.Font.Size = 12;
            r.Value2 = "Chi tiết: ";

            r = (COMExcel.Range)exSheet.get_Range("H6", "L6");
            r.Font.Bold = true;
            r.Font.Size = 12;

            exSheet.Cells[6, 8] = "STT";
            exSheet.Cells[6, 9] = "Mã hàng";
            exSheet.Cells[6, 10] = "Tên hàng";
            exSheet.Cells[6, 11] = "Số lượng";
            exSheet.Cells[6, 12] = "Đơn giá";
            int i = 7;
            foreach (DataGridViewRow row in dataGridView2.Rows)
            {
                exSheet.Cells[i, 8] = row.Cells[0].Value.ToString();
                exSheet.Cells[i, 9] = row.Cells[1].Value.ToString();
                exSheet.Cells[i, 10] = row.Cells[2].Value.ToString();
                exSheet.Cells[i, 11] = row.Cells[3].Value.ToString();
                exSheet.Cells[i, 12] = row.Cells[4].Value.ToString();
                i++;
            }

            r = (COMExcel.Range)exSheet.get_Range("J" + i.ToString(), "L" + (i + 5).ToString());
            r.Font.Bold = true;
            r.Font.Size = 12;
            if(km != "")
                exSheet.Cells[i, 10] = km;
            else
                exSheet.Cells[i, 10] = "Không có khuyến mãi";
            exSheet.Cells[i + 1, 10] = "Tổng giá trị: ";
            exSheet.Cells[i + 1, 12] = sum.ToString();
            exSheet.Cells[i + 3, 10] = "Nhân viên ký tên";

            // Giãn cột
            //r.Columns.AutoFit();

            // Hiển thị chương trình excel
            exApp.Visible = true;

            // Đóng chương trình excel
            
        }
    }
}
